<?php

namespace App\Http\Controllers\Finance;

use App\Http\Controllers\Controller;
use App\Models\FinanceSupplier;
use App\Services\SupplierService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class SupplierController extends Controller
{
    public function list(Request $request)
    {
        return $this->response->array(
            FinanceSupplier::query()
                ->when($request->get('status'), function (Builder $builder, $status) {
                    $builder->where('status', $status);
                })
                ->paginate()
        );
    }

    public function status(Request $request, $id)
    {
        $this->validate($request, [
            'status' => 'required|in:' . implode(',', [FinanceSupplier::STATUS_NORMAL, FinanceSupplier::STATUS_DISABLE])
        ], [
            'status.required' => '状态不可为空',
            'status.in' => '状态值非法'
        ]);

         FinanceSupplier::query()->findOrFail($id)->update(['status' => $request->get('status')]);

        return $this->response->noContent();
    }

    public function store(Request $request)
    {
        return $this->response->created(null, (new SupplierService())->store($request));
    }
}
